﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Text;
using SendMailLibray;
using System.Configuration;
using System.Web.Configuration;
using System.Net.Configuration;

namespace MvcSite.Models
{
    public class GestoreNewsLetter : GestoreBase
    {

        public static bool InviaNotifiche(List<string> SelectedUsers, string testo, string oggetto)
        {
            Configuration c = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
            MailSettingsSectionGroup settings = (MailSettingsSectionGroup)c.GetSectionGroup("system.net/mailSettings");
            MailSender ms = new MailSender();
            ms.From = settings.Smtp.From;
            ms.Smtp = settings.Smtp.Network.Host;
            ms.UseDefaultCredentials = settings.Smtp.Network.DefaultCredentials;
            ms.Username = settings.Smtp.Network.UserName;
            ms.Password = settings.Smtp.Network.Password;
            foreach(var item in SelectedUsers)
                ms.AddTo(item);
            ms.Subject = oggetto;
            ms.Text = testo;
            ms.EnableSsl = settings.Smtp.Network.EnableSsl;
            return ms.SendMail();
        }


        public static List<User> RecuperaUtentiIscritti(Corso Corso)
        {
            MotoreDataBase.GestoreBase.stringConnection = stringConnection;
            List<User> UtentiIscritti = new List<User>();
            UtentiIscritti = MotoreDataBase.GestoreBase.ExecuteNonQueryToList<User>("GetUtentiIscritti", 
                new MotoreDataBase.Parameter("idcorso", Corso.Id));

            return UtentiIscritti;
        }
    }
}